package com.bnlr.lrparking.app;

import android.app.ActivityManager;
import android.content.Context;
import android.provider.Settings;
import android.support.multidex.MultiDexApplication;

import com.baidu.mapapi.SDKInitializer;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.model.User;
import com.bnlr.lrparking.util.PreferencesHelper;
import com.bnlr.lrparking.util.UserModel;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.yolanda.nohttp.Logger;
import com.yolanda.nohttp.NoHttp;

import cn.jpush.android.api.JPushInterface;

/**
 * 项目名称： 利人停车 Android
 * app 程序入口
 *
 * @author licy
 */
public class Application extends MultiDexApplication {

    private static Application instance;
    public static String DEVICE_ID;
    public static IWXAPI mWxApi;
    /**
     * 用户信息
     */
    public static User mUser;
    public static PreferencesHelper mPreferenceHelper;

    public static synchronized Application getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        DEVICE_ID = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
        initCar();
    }

    private void initCar() {
        String currentProcessName = getCurrentProcessName(this);
        if (getApplicationInfo().packageName.equals(currentProcessName)) {
            // 初始化shared preference
            mPreferenceHelper = new PreferencesHelper(this, "running_config");
            // 读取用户信息
            mUser = UserModel.read();
            // 网络初始化
            NoHttp.initialize(getApplicationContext());
            Logger.setDebug(true);
            // baidu
            SDKInitializer.initialize(getApplicationContext());
            // 极光推送
            JPushInterface.setDebugMode(true);
            JPushInterface.init(this);
            // 工具包
            Utils.init(this);
            // log 打印设置
            // log 开关
            LogUtils.getConfig().setLogSwitch(true);
            // 设置log tag
            LogUtils.getConfig().setGlobalTag("TAG");
            // 注册微信
            registerToWX();
        }
    }

    /**
     * 获取当前进程名称
     *
     * @param context 上下文
     * @return String
     */
    public static String getCurrentProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

    /**
     * 注册app到微信
     */
    private void registerToWX() {
        //第二个参数是指你应用在微信开放平台上的AppID
        mWxApi = WXAPIFactory.createWXAPI(this, LrConstant.WX_APP_ID, false);
        // 将该app注册到微信
        mWxApi.registerApp(LrConstant.WX_APP_ID);
    }

}

